var now = new Date();

import dayjs from 'dayjs'

// import * as moment from 'dayjs'




function dateList() {
	//dayjs 周日是第0天
	let weekOfDay = parseInt(dayjs().day()); //计算今天是这周第几天
	// 本周六日期
	// let last_satday = moment().startOf('day').subtract(weekOfDay - 6, 'days'); //周六日期
	let last_satday = dayjs().day(6).format('YYYY-MM-DD')
	var dateList = []
	//原本是距本周六30天，现在需求是 当天+再往前数30天
	var dateListLength = 0
	if (weekOfDay == 0) {
		dateListLength = 6 + 30 - 1
	} else if (weekOfDay == 6) {
		dateListLength = 30 - 1
	} else {
		dateListLength = 6 - weekOfDay + 30 - 1
	}
	// var dateListLength = 6 - weekOfDay + 30
	for (var i = 0; i <= dateListLength; i++) {
		var item = {
			dateItem: '',
			weekDay: '',
			choose: false,
			date: '',
			chooseDate: '',
			isfuture: false,
			haveDriveData: false
		}
		if (i == 0) {
			item.dateItem = last_satday

		} else {
			// item.dateItem = moment(last_satday).subtract(i, "days");
			item.dateItem = dayjs(last_satday).subtract(i, 'day').format('YYYY-MM-DD')
		}
		// var d = item.dateItem.format("DD")
		var d = dayjs(item.dateItem).format("DD")
		if (d.substr(0, 1) == '0') {
			d = d.substr(1)
		}
		item.date = d
		// var m = item.dateItem.format("MM")
		var m = dayjs(item.dateItem).format("MM")
		if (m.substr(0, 1) == '0') {
			m = m.substr(1)
		}
		item.chooseDate = m + "月" + d + "日"
		// item.isfuture = moment().isBefore(item.dateItem)
		item.isfuture = dayjs().isBefore(dayjs(item.dateItem))
		if (getWeekDay(item.dateItem) == 1) {
			// item.weekDay = "日"
			item.weekDay = "一"
		} else if (getWeekDay(item.dateItem) == 2) {
			item.weekDay = "二"
		} else if (getWeekDay(item.dateItem) == 3) {
			item.weekDay = "三"
		} else if (getWeekDay(item.dateItem) == 4) {
			item.weekDay = "四"
		} else if (getWeekDay(item.dateItem) == 5) {
			item.weekDay = "五"
		} else if (getWeekDay(item.dateItem) == 6) {
			item.weekDay = "六"
		} else if (getWeekDay(item.dateItem) == 0) {
			item.weekDay = "日"
		}
		if (dayjs(item.dateItem).format("YYYY-MM-DD") == dayjs().format("YYYY-MM-DD")) {
			item.choose = true
		} else {
			item.choose = false
		}
		dateList.push(item)
	}
	return dateList.reverse()
}

function getWeekDay(day) {
	var weekDay = parseInt(dayjs(day).day())
	return weekDay
}

function getTrackCalendar(dateList, driveDataList) {
	for (var i = 0; i < dateList.length; i++) {
		for (var j = 0; j < driveDataList.length; j++) {
			if (dayjs(dateList[i].dateItem).format("YYYY-MM-DD") == driveDataList[j].date) {
				dateList[i].haveDriveData = true
			}
		}
	}
	return dateList
}

//获取当前时间(如果传入时间戳，返回传入时间戳当前时间)
function yearTime(e, timeStamp) {
	if (timeStamp == "") {
		timeStamp = now
	}
	var y = timeStamp.getFullYear();
	var m = timeStamp.getMonth() + 1;
	m = m < 10 ? "0" + m : m;
	var d = timeStamp.getDate();
	d = d < 10 ? "0" + d : d;

	var h = timeStamp.getHours();
	h = h < 10 ? "0" + h : h;
	var mm = timeStamp.getMinutes();
	mm = mm < 10 ? "0" + mm : mm;
	var s = timeStamp.getSeconds();
	s = s < 10 ? "0" + s : s;

	if (e == 'year') {
		return y + '/' + m + '/' + d;
	}
	if (e == 'time') {
		return h + ':' + mm + ':' + s;
	}
	if (e == 'yearTime') {
		return y + '/' + m + '/' + d + '  ' + h + ':' + mm + ':' + s;
	}
	return y + '/' + m + '/' + d;
}


module.exports = {
	dateList: dateList,
	getTrackCalendar: getTrackCalendar,
	yearTime: yearTime
}
